package com.zklh.leetcode.tree.middle;

import com.zklh.leetcode.tree.model.Node;

/**
 * @author ：tong nan
 * @date ：Created in 2021/4/21 17:51
 * @description ：连接完美二叉树实现
 * @modifiedBy ：
 */
public class ConnectTree {

    public void connect(Node root) {
        if (root.left == null) {
            return;
        }
        connect(root.left, root.right);
    }

    public void connect(Node n1, Node n2) {

        if (n1 == null && n2 == null) {
            return;
        }
        n1.next = n2;
        connect(n1.left, n1.right);
        connect(n1.right, n2.left);
        connect(n2.left, n2.right);
    }

    public static void main(String[] args) {
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.left.right = new Node(5);
        root.right.left = new Node(6);
        root.right.right = new Node(7);

        ConnectTree connectTree = new ConnectTree();
        connectTree.connect(root);
    }

}
